import {Module} from 'vuex'
import Request from '@/request';
// import {userInfoState} from '../types'
const user: Module<Record<string,any>,null> = {
    namespaced:true,
    state:{
        user:{},
    },
    actions:{
        getinfo({ dispatch,commit }) {
            /**onLaunch即调用，查看token是否过期 */
            return new Promise((resolve, reject) => {
				let token = uni.getStorageSync('token')
				if(!token){
					resolve(false)	
					return;
				}
                Request('user.getUserinfo').then(res => {
                    // console.log('usercenter.getUserInfo--',res);
                    let myData= res.data || {}
                    commit('USER_INFO', myData);
                    resolve(myData)	
                }).catch(e => {
                    reject(e)
                })
            })
        },
		logout({ dispatch,commit }) {
			uni.removeStorageSync("token");
			commit('USER_INFO', {});
        },

    },
    mutations:{
        USER_INFO(state, data){
            state.user = data;
			// console.log("USER_INFO===",state.user);
        }, 
    }
}
export default user;
